#!/bin/bash
##--此脚本用于使用rman工具热备份oracle数据库
##--需在root帐号下执行此脚本
echo "use rman to backup oracle"
backupdir=/home/rman_backup/data/$(date +%Y%m%d%H%M%S)
echo "backupdir=$backupdir"
if [ ! -d $backupdir ]; then
mkdir -p $backupdir
fi
chmod -R 755 $backupdir
chown -R oracle:oinstall $backupdir
RMAN_FILE=$backupdir/rman-backup.log
su - oracle -c "
rman log=${RMAN_FILE} <<\EOF
connect target /
run{                                                                                                                    
allocate channel ch1 device type disk;                                                                                  
backup database format='$backupdir/DB_%d_%T_%t_%U_%s_%p' 
tag='full_bak';                                                                                                         
sql 'alter system archive log current';
sql 'select checkpoint_change# from v\$database';                                                                              
crosscheck archivelog all;                                                                                                                                                                         
backup archivelog all format='$backupdir/ARCH_%d_%T_%t_%U_%s_%p' delete input  
tag='arch_bak';                                                                                                          
release channel ch1;                                                                                                    
}                                                                                                   
backup current controlfile format '$backupdir/controlfile.bak';  
backup spfile format '$backupdir/spfile.bak';
#从archivelog记录SCN
list backupset of archivelog all;
exit;
EOF
"
echo "cat ${RMAN_FILE}"
cat ${RMAN_FILE}
ls -l $backupdir/*
#其他参考命令
:<<!
su - oracle -c 'rlwrap sqlplus C##qms/windows-999'
su - oracle -c 'rlwrap rman target /'

su - oracle -c "rlwrap rman target / <<\EOF
list backupset of archivelog all;
EOF
"

su - oracle -c "rlwrap rman target / <<\EOF
list backup;
EOF
"

su - oracle -c "sqlplus / as sysdba <<\EOF
SELECT checkpoint_change#  from v\$database;
exit;
EOF
"




!